Primary exercises

  1. A function with if/else statements.

Let’s take the following from a World Health Organization document:

The categories can be enumerated as follows:

group condition for age [y]
adult age > 19
adolescent age >= 10 && age <= 19
child age >= 1 && age < 10
infant age < 1

Implement a function ageGroup which takes age argument (in years) and returns the age group label.

Function template Here is a template of the ageGroup function:

ageGroup <- function( age ) { 
  # age: age given in years
  ...
}

and here are expected results:

ageGroup(0)   # "infant"
ageGroup(10)  # "adolescent"
ageGroup(9)   # "child"
ageGroup(20)  # "adult"
ageGroup(-1)  # should generate an error

To generate the error we request the program to ?stop with an error message.



Copyright © 2022 Biomedical Data Sciences (BDS) | LUMC